package cn.zzq.aix.builder.compiler;

import java.io.File;

import cn.zzq.aix.builder.utils.Logger;
import cn.zzq.aix.builder.utils.Path;


public class GroovyCompiler extends Compiler{
	//private Path outputPath;
	//private Set<Path> sourcePath = new HashSet<Path>();
	//private Set<Path> dependencies = new HashSet<Path>();

	

	/**
	 * 构建设置编译器输出路径的命令行
	 * 
	 * @return 设置编译器输出路径的命令行
	 */
	private String buildOutputPathCommand() {
		StringBuilder sb = new StringBuilder();
		sb.append("-d ");
		//System.out.println(outputPath);
		sb.append(outputPath);
		return sb.toString();
	}

	

	private String buildClassPathCommand() {
		if (dependencies.isEmpty()) {
			return "";
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("-classpath");
			sb.append(" ");
			for (Path path : dependencies) {
				sb.append(path.toString());
				sb.append(File.pathSeparator);
			}
			sb.deleteCharAt(sb.length()-1);
			return sb.toString();
		}
	}


	private String buildSourcePathCommand() {
		if (sourcePath.isEmpty()) {
			return "";
		} else {
			StringBuilder sb = new StringBuilder();
			//sb.append("-sourcepath");
			//sb.append(" ");
			for (Path path : sourcePath) {
				sb.append(path.toString());
				sb.append(" ");
			}
			sb.deleteCharAt(sb.length()-1);
			return sb.toString();
		}
	}
	private String buildSaveParmsNameCommand() {
		if (saveParmsName) {
			return "-parameters -g";
		} else {
			return "";
		}
	}
	/**
	 * 构建完整的编译命令
	 * @return 编译命令
	 */
	private String buildAllCommand() {
		StringBuilder sb = new StringBuilder();


		sb.append(buildSourcePathCommand());	
		sb.append(" ");
		//构建源文件命令
		sb.append(buildClassPathCommand());
		sb.append(" ");
		//构建输出路径
		sb.append(buildOutputPathCommand());	

		//sb.deleteCharAt(0);
		return sb.toString();
	}
	
	/**
	 * 启动编译任务
	 */
	public void compile() {
		String command=buildAllCommand();
		Logger.log("Groovy编译器编译命令 "+command);
		String[] argv=command.split(" ");
		//PrintWriter printWriterOut=new PrintWriter(System.out);
		//PrintWriter printWriterErr=new PrintWriter(System.err);
		
		//org.codehaus.groovy.tools.FileSystemCompiler.main(argv);
	}
	
	
}
